﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Cms.Domain;

namespace Cms.Tests.Unit.Domain
{

    [TestClass]
    public class AdministratorTests
    {
        [TestMethod]
        public void TestAdministratorConstructor()
        {
            Administrator admin = new Administrator();
            DateTime now = DateTime.Now;
            Assert.AreEqual(-1, admin.SelectedMediaFolderID);
            Assert.AreEqual(-1, admin.SelectedFolderID);
            Assert.IsTrue(now.Ticks >= admin.Created.Ticks);

            //How should DateTime values be verified? The below doesn't work at all times:
            //Assert.IsTrue(now.Ticks < admin.Created.Ticks + 10000);
        }

        [TestMethod]
        public void TestFullname()
        {
            Administrator admin = new Administrator {Firstname = "Fredrik", Lastname = "Holm"};
            Assert.AreEqual("Fredrik Holm", admin.Fullname);
        }

        [TestMethod]
        public void TestLogVisitedAdminUrl()
        {
            Administrator admin = new Administrator();
            admin.LogVisitedAdminUrl("page1", "Page 1", "Page 1 image");
            admin.LogVisitedAdminUrl("page2", "Page 2", "Page 2 image");
            Assert.IsTrue(admin.VisitedAdminUrls.Count == 2);
            Assert.AreEqual("page1", admin.VisitedAdminUrls[0].Url);

            // Add an url that has already been added
            admin.LogVisitedAdminUrl("page1", "Page 1", "Page 1 image");
            Assert.IsTrue(admin.VisitedAdminUrls.Count == 2);
            Assert.AreEqual("page1", admin.VisitedAdminUrls[1].Url); // This url should now be last in the list
        }

        [TestMethod]
        public void TestLogVisitedPublicUrl()
        {
            Administrator admin = new Administrator();
            Page page1 = new Page()
            {
                ID = 1,
                Filename = "page1"
            };
            Page page2 = new Page()
            {
                ID = 2,
                Filename = "page2"
            };
            admin.LogVisitedPublicUrl(page1, "");
            admin.LogVisitedPublicUrl(page2, "");
            Assert.IsTrue(admin.VisitedPublicUrls.Count == 2);
            Assert.AreEqual("page1", admin.VisitedPublicUrls[0].Page.Filename);
            // Add a page that has already been added
            admin.LogVisitedPublicUrl(page1, "");
            Assert.IsTrue(admin.VisitedPublicUrls.Count == 2);
            Assert.AreEqual("page1", admin.VisitedPublicUrls[1].Page.Filename); // This url should now be last in the list
            // Add the same page but with a different querystring
            admin.LogVisitedPublicUrl(page1, "some querystring value");
            Assert.IsTrue(admin.VisitedPublicUrls.Count == 3);
            Assert.AreEqual("page1", admin.VisitedPublicUrls[2].Page.Filename); // This url should now be last in the list
        }
        
        [TestMethod]
        public void TestGetAndSetSearchContentSettings()
        {
            Administrator admin = new Administrator();
            SearchContentSettings settings = new SearchContentSettings
                                                 {
                                                     ContentCategoryID = 1,
                                                     LocaleID = 2,
                                                     MasterTemplateID = 3,
                                                     PageSize = 4,
                                                     ShowPreviews = true,
                                                     SortDirection = SortDirection.Descending
                                                 };
            admin.SetSearchContentSettings(settings);
            SearchContentSettings decodedSettings = admin.GetSearchContentSettings();
            Assert.AreEqual(settings.ContentCategoryID, decodedSettings.ContentCategoryID);
            Assert.AreEqual(settings.LocaleID, decodedSettings.LocaleID);
            Assert.AreEqual(settings.MasterTemplateID, decodedSettings.MasterTemplateID);
            Assert.AreEqual(settings.PageSize, decodedSettings.PageSize);
            Assert.AreEqual(settings.ShowPreviews, decodedSettings.ShowPreviews);
            Assert.AreEqual(settings.SortDirection, decodedSettings.SortDirection);
        }
    }
}
